// components/comment/index.js
import { Comment } from "../../model/Comment";
import { Utils } from "../../utils/util";
import { Config } from "../../utils/config";
import { User } from "../../model/User.js";
const comment = new Comment();
const utils = new Utils();
const user = new User();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        comment: {
            type: Object,
            value: {}
        },
        type: {
            type: String,
            value: "detail"
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        cid: 0,
        replayState: 'open'
    },
    lifetimes: {

    },
    /**
     * 组件的方法列表
     */
    methods: {
        /**
         * 评论点赞
         */
        thumb(e) {
            const id = e.currentTarget.dataset.cid;
            const type = e.currentTarget.dataset.type;
            const replayindex = e.currentTarget.dataset.index;
            const state = type == 'like' ? 1 : 0;
            const num = type=='like'?1:-1;
            comment.thumb({
                    type: type,
                    id: id
                })
                .then(res => {
                    if (replayindex == "N") {
                        this.setData({
                            ['comment.LoggerIsLike']: state,
                            ['comment.LikeNum']:this.data.comment.LikeNum*1+num
                        });
                    } else {
                        if (this.data.type == "detail") {
                            let newReplies = this.data.comment['Replies'].map((replay, index) => {
                                if (index == replayindex) {
                                    replay.LoggerIsLike = state
                                    replay.LikeNum = replay.LikeNum*1+num;
                                }
                                return replay;
                            });
                            this.setData({
                                ['comment.Replies']: newReplies
                            });
                        } else {
                            this.setData({
                                ['comment.FirstReply.LoggerIsLike']: state,
                                ['comment.FirstReply.LikeNum']:this.data.comment.FirstReply.LikeNum*1+num
                            });
                        }
                    }
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: 'none'
                    });
                });
        },
        /**
         * 点击评论内容
         */
        touchComment(e) {
            if (this.data.type == "list") {
                this.gotoMoreComment();
                return false;
            }
            const cid = e.currentTarget.dataset.cid;
            const fromUid = e.currentTarget.dataset.fromuid;
            const nick = e.currentTarget.dataset.nick;
            console.log(nick);
            const localUid = wx.getStorageSync("uId");
            const isself = fromUid == localUid;
            let itemList = ["举报"]
            if (fromUid == localUid) {
                itemList.unshift("删除")
            } else {
                itemList.unshift("回复")
            }
            wx.showActionSheet({
                itemList: itemList,
                success: res => {
                    if (res.tapIndex == 1) {
                        this.report(cid);
                    } else {
                        if (isself) {
                            this._deleteComment(cid);
                        } else {
                            this.triggerEvent("replay", {
                                cid: cid,
                                nick:nick
                            });
                        }
                    }
                }
            })
        },
        /**
         * 点击预览图片或视频
         */
        lookMedia(e) {
            const type = e.currentTarget.dataset.type;
            const url = e.currentTarget.dataset.url;
            if (type == "picture") {
                utils.previewImage(0, [url]);
            } else {
                wx.navigateTo({
                    url: `/pages/video/index?url=${url}`
                });
            }
        },
        /**
         * 删除评论
         */
        _deleteComment(id) {
            comment.delete(id)
                .then(res => {
                    wx.showToast({
                        title: "删除成功"
                    })
                    setTimeout(() => {
                        this.triggerEvent("reloadcomment");
                    });
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: "none"
                    });
                });
        },
        /**
         * 二级评论展开与关闭
         */
        changeReplayState() {
            this.setData({
                replayState: this.data.replayState == "open" ? "close" : "open"
            });
        },
        /**
         * 举报
         */
        report(cid) {
            let itemList = ["恶意引战", "劣质广告", "日经贴, 重复太多", "拒绝黄赌毒", "包含政治敏感信息", "其他理由"];
            wx.showActionSheet({
                itemList: itemList,
                success: function(e) {
                    let options = {
                        id: cid,
                        type: 2,
                        reason: e.tapIndex + 1
                    }
                    user.report(options)
                        .then(res => {
                            wx.showToast({
                                title: '举报成功',
                            })
                        })
                        .catch(err => {
                            wx.showToast({
                                title: err.msg,
                                icon: "none"
                            })
                        });
                }
            })
        },
        /**
         *查看更多评论
         */
        gotoMoreComment() {
            const cid = this.data.comment.CommentId;
            wx.navigateTo({
                url: `/pages/detail/index?id=${this.data.comment.PostId}&cid=${cid}`,
            })
        }
    }
})